﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace PhiStore.Storage.UI
{
    internal partial class StorageOverviewUI : Form
    {
        public StorageOverviewUI()
        {
            InitializeComponent();

            StorageList.ColumnCount = 5;

            StorageList.Columns[0].Name = "Storage";
            StorageList.Columns[1].Name = "Limit";
            StorageList.Columns[2].Name = "Remaining";
            StorageList.Columns[3].Name = "Working";

            foreach (DataGridViewColumn col in StorageList.Columns)
            {
                col.SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            VerifyUI();
        }

        List<IStorageDevice> list = new List<IStorageDevice>();

        private void VerifyUI()
        {

            list = new List<IStorageDevice>(StorageManager.Instance.Devices);
            StorageList.Rows.Clear();

            long totalReserved = 0L;
            long totalRemaining = 0L;
            string[] row;
            foreach (IStorageDevice device in list)
            {
                row = new string[] { 
                    device.ToString(), 
                    (device.ReservedBytes/1024/1024).ToString() + " MB", 
                    (device.RemainingBytes/1024/1024).ToString()+ " MB",
                    device.Works() ? "Yes" : "No",
                };
                totalReserved += device.ReservedBytes;
                totalRemaining += device.RemainingBytes;
                StorageList.Rows.Add(row);
            }

            row = new string[] {
                "Total",
                (totalReserved/1024/1024).ToString() + " MB",
                (totalRemaining/1024/1024).ToString() + " MB",
                StorageManager.Instance.ReadyToRun ? "Yes" :  "No"
            };
            StorageList.Rows.Add(row);
        }

        private void AddDevice_Click(object sender, EventArgs e)
        {
            StorageUI dialog = new StorageUI();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (StorageManager.Instance.Add(dialog.Device))
                    VerifyUI();
            }
        }

        private void StorageList_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (StorageList.HitTest(e.X, e.Y).RowIndex >= 0
                && EditDevice.Enabled)
            {
                EditDevice_Click(sender, e);
            }
        }

        private void EditDevice_Click(object sender, EventArgs e)
        {
            if (StorageList.SelectedCells.Count > 0 && StorageList.SelectedCells[0].RowIndex < list.Count)
            {
                StorageUI dialog = new StorageUI();
                IStorageDevice selected =  list[StorageList.SelectedCells[0].RowIndex];
                dialog.Device = selected;
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    StorageManager.Instance.Remove(selected);
                    StorageManager.Instance.Add(dialog.Device);
                    VerifyUI();
                }
            }

        }

        private void DeleteDevice_Click(object sender, EventArgs e)
        {
            if (StorageList.SelectedCells.Count > 0 && StorageList.SelectedCells[0].RowIndex < list.Count)
            {
                IStorageDevice device = list[StorageList.SelectedCells[0].RowIndex];

                StorageManager.Instance.Remove(device);
                VerifyUI();
            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK ==
                MessageBox.Show("This will remove any objects currently stored in the devices.", "Clear?", MessageBoxButtons.OKCancel))
            {
                
                StorageManager.Instance.ClearDevices();
                VerifyUI();
            }
        }
    }
}
